package com.example.medmobile.profissional;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import utils.BaseActivity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;

import com.example.medmobile.HorarioAgendamento;
import com.example.medmobile.R;

public abstract class CaixaEntradaProfissional extends BaseActivity {

	protected ListView listView;
	protected List<com.google.appengine.medmobileserver.business.Agendamento> li;

	protected ProgressDialog dialog;
	protected Handler handler = new Handler();

	private int mYear;
	private int mMonth;
	private int mDay;
	private EditText filtroBusca;
	static final int DATE_DIALOG_ID = 0;
	private String sDataCorrente;
	private Dialog dia;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_caixa_entrada_profissional);

		listView = (ListView) findViewById(R.id.CaixaEntradaListView);
		filtroBusca = (EditText) findViewById(R.id.CaixaEntradaDataSelecao);
		Calendar c = Calendar.getInstance();
		sDataCorrente = new SimpleDateFormat("dd/MM/yyyy").format(c.getTime())
				.toString();
		filtroBusca.setText(sDataCorrente);
		this.mYear = c.get(Calendar.YEAR);
		this.mMonth = c.get(Calendar.MONTH);
		this.mDay = c.get(Calendar.DAY_OF_MONTH);

		filtroBusca.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				showDialog(DATE_DIALOG_ID);
			}
		});

		CarregaLista(sDataCorrente, this);

	}

	protected abstract void CarregaLista(String sData, Context context);

	// the callback received when the user "sets" the date in the dialog
	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {

			if (mYear == year && mMonth == monthOfYear + 1
					&& mDay == dayOfMonth)
				return;


			mYear = year;
			mMonth = monthOfYear + 1;
			mDay = dayOfMonth;

			NumberFormat formatter = new DecimalFormat("00");
			sDataCorrente = formatter.format(mDay) + "/"
					+ formatter.format(mMonth) + "/" + mYear;

			String sData = formatter.format(mDay) + formatter.format(mMonth)
					+ mYear;
			
				CarregaLista(sData, CaixaEntradaProfissional.this);
				filtroBusca.setText(sDataCorrente);
	

		}
	};

	@Override
	protected Dialog onCreateDialog(int id) {
		DatePickerDialog d = null;
		switch (id) {
		case DATE_DIALOG_ID:
			d = new DatePickerDialog(this, mDateSetListener, this.mYear,
					this.mMonth, this.mDay);

		}

		return d;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.caixa_entrada_profissional, menu);
		return true;
	}

}
